package com.solarfridge.monitor.client;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
import com.solarfridge.monitor.shared.data.Fridge;
import com.solarfridge.monitor.shared.data.FridgeData;

/**
 * Handles querying the server for data.
 */
@RemoteServiceRelativePath("DataService")
public interface DataService extends RemoteService {
	
	/** Impliesa successful call **/
	public static int SUCCESS = 0;
	public static int NAME_TAKEN = SUCCESS + 1;
	public static int NO_FRIDGE_FOUND = NAME_TAKEN + 1;
	
	/** Gets the number of fridges maintined by
	 * the site */
	int getNumFridges();
	/**
	 * @return A list of fridges
	 */
	Fridge[] getFridgeList();
	/**
	 * Gets the fridge object corresponding
	 * to a given fridgeId.
	 * @return null is no fridge by the given
	 * id exists.
	 */
	Fridge getFridgeById(String id);
	
	/** Saves the specified fridge to persistent
	 * memory. Assumes only one Fridge exists
	 * per id.
	 * @param fridgeToSave
	 * @return see return codes for {@link DataService}
	 * 
	 */
	int saveFridge(Fridge fridgeToSave);
	
	/**
	 * Updates a fridge to reflect changes in metadata.
	 * @param updateFridge
	 * @return
	 */
	int updateFridge(Fridge updateFridge);
	
	/**
	 * Records the data from the current state of the supplied fridgeData
	 * object. NOTE: this function records the data for a fridge
	 * regardless of the logging status of the fridge. It does,
	 * however, check to ensure that a fridge by the given id
	 * exists before logging the data.
	 * @return see return codes for {@link DataService}
	 */
	int recordData(FridgeData dataToRecord);
	
	/**
	 * Returns the most recent FridgeData records coresponding
	 * to a given FridgeId
	 * @param fridgeId
	 * @param numRecords The number of records to return.
	 * @return An empty array if no records found, or the
	 * id does not exist.
	 */
	List<FridgeData> getRecentData(String fridgeId, int numRecords);
	
	/**
	 * Utility class for simplifying access to the instance of async service.
	 */
	public static class Util {
		private static DataServiceAsync instance;
		public static DataServiceAsync getInstance(){
			if (instance == null) {
				instance = GWT.create(DataService.class);
			}
			return instance;
		}
	}
}
